#pragma once

#include <YSimpleNetwork/INetworkFactory.h>
#include <concurrent_queue.h>
#include <WinSock2.h>
#include <ppl.h>
#include <agents.h>

namespace ysimple_network
{
	class YNetworkFactory 
		: public INetworkFactory
	{
		Concurrency::task_group m_tasks;

		struct DisconnectedSocketDesc
		{
			SOCKET m_socket;
			DWORD64 m_disconnectedTick;
			DisconnectedSocketDesc():m_disconnectedTick(0){}
			DisconnectedSocketDesc(SOCKET socket): m_socket(socket), m_disconnectedTick(::GetTickCount()){}
		};

		Concurrency::unbounded_buffer<DisconnectedSocketDesc> m_Time_Wait_State_Pool;	

		concurrency::concurrent_queue<SOCKET>		m_serverSockets;
		concurrency::concurrent_queue<SOCKET>		m_clientSockets;
		concurrency::concurrent_queue<SOCKET>		m_udpSockets;
	public:
		YNetworkFactory();
		virtual ~YNetworkFactory(){}

		TCP_SESSION_SPTR		createProactiveClientTCPSession(ysimple_system::IProactor& proactor) override;
		TCP_SESSION_SPTR		createProactiveServerTCPSession(ysimple_system::IProactor& proactor) override;
		UDP_SESSION_SPTR	createProactiveUDPSession(ysimple_system::IProactor& proactor) override;
		PACKET_SPTR			createPacket(size_t len) override;
	};

}

